﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Collections.Generic;
using System.Xml;

namespace Fulcrum.ControlGenerator
{
    /// <summary>
    /// Configuration data about a given dependency property
    /// </summary>
    public abstract class BaseDependencyPropertyXmlConfiguration
    {
        List<string> metadataOptions = new List<string>();

        protected BaseDependencyPropertyXmlConfiguration(XmlReader xmlReader)
        {
            // TODO Name should never be null or empty
            // TODO Type should never be null or empty
            // TODO DefaultValue should never be null or empty
            this.Name = xmlReader.GetAttribute("name");
            this.Type = xmlReader.GetAttribute("type");
            this.DefaultValue = xmlReader.GetAttribute("defaultValue");
            this.IsReadOnly = XmlReaderHelp.GetOptionalBoolAttributeValue(xmlReader, "isReadOnly", false);

            this.CoerceValue = XmlReaderHelp.GetOptionalBoolAttributeValue(xmlReader, "coerceValue", false);
            this.ValidateValue = XmlReaderHelp.GetOptionalBoolAttributeValue(xmlReader, "validateValue", false);

            var visibility = XmlReaderHelp.GetOptionalAttributeValue(xmlReader, "visibility", "Public");
            this.Visibility = (PropertyVisibility) Enum.Parse(typeof(PropertyVisibility), visibility, true);

            ProcessMetadataOptions(xmlReader);
        }

        private void ProcessMetadataOptions(XmlReader xmlReader)
        {
            string metadataOptionsString = xmlReader.GetAttribute("metadataOptions");

            if (false == String.IsNullOrEmpty(metadataOptionsString))
            {
                var options = metadataOptionsString.Split('|');

                // TODO - Trim each option
                foreach (var option in options)
                {
                    metadataOptions.Add(option);
                }

                metadataOptions.Sort(StringComparer.InvariantCulture);
            }
        }

        public string Name
        {
            get;
            private set;
        }

        public string Type
        {
            get;
            private set;
        }

        public string DefaultValue
        {
            get;
            private set;
        }

        public bool IsReadOnly
        {
            get;
            private set;
        }

        public PropertyVisibility Visibility
        {
            get;
            private set;
        }

        public bool CoerceValue
        {
            get;
            private set;
        }

        public bool ValidateValue
        {
            get;
            private set;
        }

        public ICollection<string> MetadataOptions
        {
            get
            {
                return this.metadataOptions;
            }
        }

    }
}
